package ShoppingCartJFrame;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;

import javax.swing.*;

import shoppingCart.ShoppingCart;
import shoppingCart.ShoppingCartEvent;
import shoppingCart.ShoppingCartItem;


@SuppressWarnings("serial")
public class ShoppingCartUI extends JFrame implements Observer, ActionListener{
	protected ShoppingCart cart;
	protected ObjectList itemList;
	protected JTextField customerName;
	protected JTextField totalField;
	
	private JButton removeItem = new JButton("Remove Item");
	private JButton placeOrder = new JButton("Place Order");
	
	public ShoppingCartUI()
	{
	
		// Make this class an observer of the shopping cart
				cart = new ShoppingCart();
				cart.addObserver(this);

		// Create the list of objects in the cart
				itemList = new ObjectList();

		// Create the field for the total cost so far
				totalField = new JTextField(10);
				totalField.setEditable(false);
				totalField.setText("Total: "+cart.total);

				setLayout(new BorderLayout());

		// Create a field for the customer name
				customerName = new JTextField(20);

		// Combine the label and the name field on a single panel
				JPanel namePanel = new JPanel();
				namePanel.add(new JLabel("Customer Name: "));
				namePanel.add(customerName);

		// Put the name field up at the top and the item list in the center
				add("North", namePanel);
				add("Center", itemList);

		// Create buttons for removing items and placing an order and put
		// them along the bottom.

				JPanel southPanel = new JPanel();
				southPanel.add(removeItem);
						/*new CommandButton(
					"Remove Item",
					new ShoppingCartRemove(this)));*/
				southPanel.add(placeOrder);
						/*new CommandButton(
					"Place Order",
					new ShoppingCartOrder(this)));*/
				southPanel.add(totalField);

				add("South", southPanel);

		// Tell the frame registry about this frame
				FrameRegistry.instance().addFrame("Shopping Cart", this);
				
				removeItem.addActionListener(this);
				placeOrder.addActionListener(this);
				
				setSize(460, 500);
				setVisible(true);
	}
	
	public String makeItemString(ShoppingCartItem item)
	{
		String itemString = ""+ item.itemName + "\t" + "Qty: " + item.quantity + 
				"\t" + "Cost: " + item.itemCost;
		return itemString;
	}
	
	public void update(Observable whichCart, Object ob)
	{
		ShoppingCartEvent event = (ShoppingCartEvent) ob;

		if (event.eventType == ShoppingCartEvent.ADDED_ITEM) {
// If there is a new item in the cart, add it to the scrollable list
			itemList.addObject(makeItemString(event.item),
				event.item);
			totalField.setText("Total: "+cart.total);
			itemList.validate();
		} else if (event.eventType ==
// If an item has been removed from the cart, remove it from the list
			ShoppingCartEvent.REMOVED_ITEM) {
			itemList.delObject(event.item);
			totalField.setText("Total: "+cart.total);
			itemList.validate();
		} else if (event.eventType ==
			ShoppingCartEvent.CHANGED_ITEM) {
// If an item has changed, update the list
			int index = itemList.indexOf(event.item);
			itemList.replaceObject(makeItemString(
				event.item), event.item, index);
			totalField.setText("Total: "+cart.total);
			itemList.validate();
		}
	}
	
	// If the user clicks on "Remove Item," remove it from he list
		public void doRemove()
		{
			Object ob = itemList.getSelectedObject();
			if (ob == null) return;

			ShoppingCartItem item = ((ShoppingCartItem)ob).copy();
			item.quantity = 1;
			cart.removeItem(item);
		}
		
		// doPlaceOrder uses PostSockURL to post the order to a web
		// server. You will need to customize this method to fit your needs.

			public void doPlaceOrder()
			{
			/*	try {
					URL postURL = new URL(
						getDocumentBase().getProtocol(),
						getDocumentBase().getHost(),
						getDocumentBase().getPort(),
						"/shopping");

					ByteArrayOutputStream byteOut = 
						new ByteArrayOutputStream();
					PrintStream outStream = 
						new PrintStream(byteOut);

					outStream.println("Custname: "+
						customerName.getText());
					ShoppingCartItem[] items = cart.getItems();
					for (int i=0; i < items.length; i++) {
						outStream.println(
						items[i].itemName+"|"+
						items[i].quantity);
					}

					String response = PostSockURL.post(postURL,
						byteOut.toString());
					System.out.println(response);
				} catch (Exception e) {
					e.printStackTrace();
				}
					*/
			}

			public ShoppingCart getShoppingCart()
			{
				return cart;
			}


			public void actionPerformed(ActionEvent evt) {
				if (evt.getSource() == removeItem){
					new ShoppingCartRemove(this);
				}
				else if (evt.getSource() == placeOrder)
				{
					new ShoppingCartOrder(this);
				}
			}
	

}
